package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.SmsSpikeActivity;
import com.mall.service.SmsSpikeActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "秒杀活动管理")
@RestController
@RequestMapping("/spikeActivities")
public class SmsSpikeActivityController {

    @Resource
    private SmsSpikeActivityService smsSpikeActivityService;

    @ApiOperation(value = "新增或修改秒杀活动")
    @ApiParam(name="spikeActivity",value="秒杀活动，用于新增或修改")
    @PostMapping
    public Result save(@RequestBody SmsSpikeActivity smsSpikeActivity) {
        boolean flag;
        if(smsSpikeActivity.getCreateTime()==null)
        {
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String current = formatter.format(date);
            smsSpikeActivity.setCreateTime(current);
        }
        flag = smsSpikeActivityService.Save(smsSpikeActivity);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }


    @ApiOperation(value = "根据id删除秒杀活动")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsSpikeActivityService.delete(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsSpikeActivityService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查询活动")
    @ApiParam(name = "id",value="秒杀活动id，用于查询特定秒杀活动")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsSpikeActivity smsSpikeActivity = new SmsSpikeActivity();
        smsSpikeActivity = smsSpikeActivityService.getById(id);
        Integer code = smsSpikeActivity !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsSpikeActivity !=null ? "successful":"failed,please try again";
        return new Result(smsSpikeActivity,code,msg);
    }

    @ApiOperation(value = "查询全部活动")
    @GetMapping
    public Result getAll(){
        List<SmsSpikeActivity> list = smsSpikeActivityService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "分页查询全部活动")
    @GetMapping("/page")
    public IPage<SmsSpikeActivity> getPage(@RequestParam Integer pageNum,
                                           @RequestParam Integer pageSize,
                                           @RequestParam String title) {
        IPage<SmsSpikeActivity> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsSpikeActivity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return smsSpikeActivityService.page(page,queryWrapper);
    }


    @ApiOperation(value = "根据id改变活动状态")
    @ApiParam(name = "id",value = "秒杀活动id，用于修改活动状态")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id) {
        SmsSpikeActivity smsSpikeActivity = smsSpikeActivityService.getById(id);
        if(smsSpikeActivity.getStatus()==1)
        {
            smsSpikeActivity.setStatus(0);
        }else{
            smsSpikeActivity.setStatus(1);
        }
        boolean flag = smsSpikeActivityService.Save(smsSpikeActivity);
        return new Result(flag?Code.UPDATE_OK: Code.UPDATE_ERR,flag);
    }
}
